<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/9
 * Time: 11:27
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class ProjectModel
{
    const TABLE = 'project';

    /**
     * @param array $data
     * @return mixed
     */
    public function selectLists(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function($query)use($data){
                if (isset($data['isAll']) && $data['isAll'] !=1){
                    $query->where('id', '>', 1);
                }
                if (!empty($data['status'])){
                    $query->where('status',$data['status']);
                }
            })
            ->orderBy('status')
            ->orderBy('order', 'desc')
            ->orderBy('createTime', 'desc')
            ->get()->toArray();
    }

    /**
     * 项目信息
     * @param array $where
     * @return mixed
     */
    public function info(array $where)
    {
        $result = DB::table(self::TABLE)->where($where)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function lists(array $data)
    {
        $limit = config('yucheng.limit');
        $start = 0;
        if (!empty($data['length'])) {
            $limit = $data['length'];
        }
        if (!empty($data['page'])) {
            $start = ($data['page'] - 1) * $limit;
        }

        return DB::table(self::TABLE)
            ->where(function ($query) use ($data) {
                $query->where('id', '!=', 1);
                if (isset($data['search']) && !is_null($data['search'])) {
                    $query->where(self::TABLE . '.name', 'like', '%' . $data['search'] . '%');
                }
            })
            ->orderBy('status')
            ->orderBy('order', 'desc')
            ->orderBy('createTime', 'desc')
            ->offset($start)->limit($limit)
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function countLists(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($data) {
                $query->where('id', '!=', 1);
                if (isset($data['search']) && !is_null($data['search'])) {
                    $query->where(self::TABLE . '.name', 'like', '%' . $data['search'] . '%');
                }
            })
            ->count();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        $data['createTime'] = date('Y-m-d H:i:s');
        $data['code'] = $this->generateCode();
        return DB::table(self::TABLE)->insertGetId($data);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where,array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function delete(array $data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }

    private function generateCode()
    {
        $s = "B";
        $lastCode = DB::table(self::TABLE)->orderBy('code','desc')->value('code');
        $lastCode = empty($lastCode)?'000':$lastCode;
        $number = intval(substr($lastCode,1))+1;
        if (0<$number && $number <10){
            $code = $s.'00'.$number;
        }elseif(9<$number && $number <100){
            $code = $s.'0'.$number;
        }elseif(99<$number && $number <1000){
            $code = $s.$number;
        }
        return $code;
    }
}